package com.rykj.widget.bottomnavigation;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * description:
 * @author ycw
 */
public class NavigationBottomView extends LinearLayout {

    private List<BottomViewModle> modles = null;

    private SparseArray<ItemView> itemViews = null;

    private BottomObservable bottomObservable = new BottomObservable();
    private OnChangeStatus onChangeStatus;

    public NavigationBottomView(Context context) {
        this(context, null);
    }

    public NavigationBottomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBottomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initial(context);
    }

    private void initial(Context context) {
        setClipChildren(false);
        setOrientation(HORIZONTAL);
    }

    public void setModles(List<BottomViewModle> modles) {
        this.modles = modles;
        updateTextOrDrawable(modles);
        invalidate();
    }

    /**
     *
     */
    private void updateTextOrDrawable(List<BottomViewModle> modles) {
        if (modles != null) {
            int size = modles.size();

//            if (itemViews != null) {
            int cacheSize = generateChildView().size();
            if (cacheSize > size) {
                for (int i = size; i < cacheSize; i++) {
                    removeView(generateChildView().get(i));
                    generateChildView().removeAt(i);
                }
            }
//            }

            for (int i = 0; i < size; i++) {
                ItemView itemView = createItemView(i);
                itemView.setText(modles.get(i).getNavText());
                itemView.setImage(modles.get(i).getNavDrawable());
                if (!isAdd(i)) {
                    addView(itemView, generateLayoutParams());
                }
                generateChildView().put(i, itemView);
            }
            setOnChangeListener();
        } else {
            removeAllViews();
        }
    }

    /**
     * @return 当前的子view的集合
     */
    public SparseArray<ItemView> generateChildView() {
        if (itemViews == null) {
            itemViews = new SparseArray<>();
        }
        return itemViews;
    }

    private ItemView createItemView(int index) {
        ItemView itemView = null;
        if (generateChildView() != null) {
            itemView = generateChildView().get(index);
        }
        if (itemView == null) {
            itemView = new ItemView(getContext());
        }
        return itemView;
    }

    private boolean isAdd(int index) {
        if (generateChildView() != null && generateChildView().get(index) != null) {
            return true;
        } else {
            return false;
        }
    }

    public LayoutParams generateLayoutParams() {
        return new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
    }

    private void setOnChangeListener() {
        int size = generateChildView().size();
        for (int i = 0; i < size; i++) {
            ItemView itemView = generateChildView().get(i);
            bottomObservable.addObserver(itemView);
            itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2017/10/31 点击事件触发改变view选中状态
                    if (onChangeStatus != null) {
                        ItemView value = (ItemView) v;
                        int position = generateChildView().indexOfValue(value);
                        boolean goNext = onChangeStatus.change(position, value);
                        if (goNext) {
                            changeViewStatus((BottomObserver) v);
                        }
                    }
                }
            });
        }
    }

    /**
     * 改变 view 的选中状态
     */
    public void changeViewStatus(BottomObserver v) {
        bottomObservable.notifyObservers(v);
    }

    public void setOnChangeStatus(OnChangeStatus onChangeStatus) {
        this.onChangeStatus = onChangeStatus;
    }

    public interface OnChangeStatus {
        /**
         * 状态改变事件触发
         * @param position
         *         改变状态的 view 的位置
         * @param view
         *         改变状态的 view
         * @return true 改变 view 选中状态 ; false  不改变 view 选中状态
         */
        boolean change(int position, ItemView view);
    }

    public interface BottomViewModle {

        /**
         * 返回底部文字
         * @return CharSequence {@link CharSequence}
         */
        CharSequence getNavText();

        /**
         * 返回底部的图片
         * @return Drawable {@link Drawable}
         */
        Drawable getNavDrawable();
    }

}
